package com.aivin.lib_reader.view;


import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.SeekBar;

import com.aivin.lib_reader.R;
import com.aivin.lib_reader.icallback.SettingViewCallBack;


public class SettingView extends FrameLayout {

    private boolean isShowing;

    public SettingView( Context context) {
        this(context, null);
    }

    public SettingView( Context context,  AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SettingView( final Context context,  AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.reader_setting_layout, this);
        setVisibility(GONE);

        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        initView() ;
    }


    private void initView(){
        findViewById(R.id.reader_bg_0).setOnClickListener(onClickListener);
        findViewById(R.id.reader_bg_2).setOnClickListener(onClickListener);

        SeekBar textSizeSeekBar = findViewById(R.id.text_size_seek_bar);
        textSizeSeekBar.setMax(100);
        textSizeSeekBar.setOnSeekBarChangeListener( onSeekBarChangeListener);

        SeekBar textSpaceSeekBar = findViewById(R.id.text_space_seek_bar);
        textSpaceSeekBar.setMax(200);
        textSpaceSeekBar.setOnSeekBarChangeListener( onSeekBarChangeListener);
    }


    private SeekBar.OnSeekBarChangeListener onSeekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            int i = seekBar.getId();
            if (i == R.id.text_size_seek_bar) {
                if (settingViewCallBack != null) {
                    settingViewCallBack.onChangeTextSize(progress + 20);//文字大小限制最小20
                }
            }

            else if (i == R.id.text_space_seek_bar) {
                if (settingViewCallBack != null) {
                    settingViewCallBack.onChangeLineSpage(progress);
                }
            }


        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    };


    private OnClickListener  onClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            int i = v.getId();
            if (i == R.id.reader_bg_0) {
                if (settingViewCallBack != null) {
                    settingViewCallBack.onChangeBgColor(getResources().getColor(R.color.reader_bg_3));
                }
            }

            else if (i == R.id.reader_bg_2) {
                if (settingViewCallBack != null) {
                    settingViewCallBack.onChangeBgColor(getResources().getColor(R.color.reader_bg_2));
                }
            }
        }
    };
    public void dismiss() {
        setVisibility(View.GONE);
        isShowing = false;
    }

    public void show() {
        setVisibility(View.VISIBLE);
        isShowing = true;
    }

    private SettingViewCallBack settingViewCallBack  ;

    public void setSettingViewCallBack(SettingViewCallBack settingViewCallBack) {
        this.settingViewCallBack = settingViewCallBack;
    }

    public boolean isShowing() {
        return isShowing;
    }
}
